<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\PlaceModel;
use App\Service\CommonService;
use Illuminate\Http\Request;
use Mockery\Exception;
//何家令 11.18
class PlaceController extends Controller
{
    public function getPlace()
    {
        $data=PlaceModel::get();
        if(!$data) return CommonService::fail(9999,'地址查询失败',null);
        return CommonService::success(200,'地址查询成功',$data);
    }
    public function placeDetail(Request $request)
    {
        $id=intval($request->get('id'));
        $data=PlaceModel::where('id',$id)->get();
        if(!$data) return CommonService::fail(9999,'地址查询失败',null);
        return CommonService::success(200,'地址查询成功',$data);
    }
    //添加地址
    public function addPlace(Request $request)
    {

      $address_name=strval($request->get('address_name'));
      $address_phone=intval($request->get('address_phone'));
      $address_rid=intval($request->get('address_rid'));
      $address_text=strval($request->get('address_text'));

        try {
            if(empty($address_name)||empty($address_phone)||empty($address_rid)||empty($address_text)) return CommonService::fail(9669,'数据不能为空',null);
            $res=PlaceModel::create([
                'address_name'=>$address_name,
                'address_phone'=>$address_phone,
                'address_text'=>$address_text,
                'address_rid'=>$address_rid
            ]);
            if ($res) return CommonService::success(200, '地址添加成功', null);
            return CommonService::fail(9999, '地址添加失败', null);
        }catch (Exception $exception) {
            return CommonService::fail(9999, $exception->getMessage(), null);
        }
    }
    public function updatePlace(Request $request)
    {
        $address_name=strval($request->get('address_name'));
        $address_text=strval($request->get('address_text'));
        $address_phone=intval($request->get('address_phone'));
        $id=intval($request->get('id'));
        try {
        if(empty($address_name)||empty($address_phone)||empty($id)||empty($address_text)) return CommonService::fail(9669,'数据不能为空',null);
        $res=PlaceModel::where('id',$id)->update([
            'address_name'=>$address_name,
            'address_text'=>$address_text,
            'address_phone'=>$address_phone
        ]);
        if ($res) return CommonService::success(200, '地址修改成功', null);
        return CommonService::fail(9999, '地址修改失败', null);
       }catch (Exception $exception) {
      return CommonService::fail(9999, $exception->getMessage(), null);
    }

    }
    public function delPlace(Request $request)
    {
        $id = $request->get('id');
        try {
            if (empty($id)) return CommonService::fail(9999, '删除id不能为空', null);
            $res = PlaceModel::where('id', $id)->delete();
            if ($res) return CommonService::success(200, '地址删除成功', null);
            return CommonService::fail(9999, '地址删除失败', null);
        } catch (Exception $exception) {
            return CommonService::fail(9999, $exception->getMessage(), null);

        }
    }

}
